<template>
  <div class="td-doctor-me-appointment-item-edit">
    <mt-header fixed title="预约项目设置">
      <router-link to="tdDoctorMeAppointmentItem" slot="left">
        <mt-button icon="back"></mt-button>
      </router-link>
    </mt-header>

    <mt-field label="项目名称" placeholder="请输入项目名称" v-model="params.ItemName"></mt-field>
    <mt-field label="价格" placeholder="请输入价格" v-model="params.ItemPrice">&nbsp;元</mt-field>
    <mt-cell title="状态">
      <span>
        <el-radio-group size="mini" v-model="params.ItemStatus">
          <el-radio-button label="0">启用</el-radio-button>
          <el-radio-button label="1">禁用</el-radio-button>
        </el-radio-group>
      </span>
    </mt-cell>

    <div class="btnSubmit">
      <el-button
        type="primary"
        class="td-btn-doctor-primary"
        style="width:80%;"
        round
        @click.native="btnSave();"
      >保存</el-button>
    </div>
  </div>
</template>

<script>
import DoctorAppointmentItemService from "../../../../_services/doctorAppointmentItem.service";
export default {
  name: "TianDingDoctorMeAppointmentItemEditComponent",
  data() {
    return {
      user: {},
      params: {
        AppointmentItemID: 0,
        DoctorID: 0,
        ItemName: "",
        ItemPrice: "0.00",
        ItemStatus: "0"
      }
    };
  },
  mounted() {
    this.user = this.utils$.dataFormat(
      JSON.parse(this.local$.getItem("tdUser"))
    );
    if (this.utils$.isEmpty(this.local$.getItem("tdAppointmentItem"))) {
      this.params = JSON.parse(this.local$.getItem("tdAppointmentItem"));
    }
  },
  methods: {
    btnSave() {
      if (!this.utils$.isEmpty(this.params.ItemName)) {
        this.toast$("请输入项目名称");
        return;
      }

      if (!this.utils$.isEmpty(this.params.ItemPrice)) {
        this.toast$("请输入价格");
        return;
      }

      this.indicator$.open("请稍后...");

      let params = {
        AppointmentItemID: this.params.AppointmentItemID,
        DoctorID: this.user.DoctorID,
        ItemName: this.params.ItemName,
        ItemPrice: parseFloat(this.params.ItemPrice).toString() * 100,
        ItemStatus: parseInt(this.params.ItemStatus)
      };

      if (params.AppointmentItemID > 0) {
        DoctorAppointmentItemService.updateAppointmentItem(params).then(
          response => {
            if (response !== null && response.Data > 0) {
              this.toast$("保存成功");
            } else {
              this.toast$("保存失败");
            }
            this.indicator$.close();
            this.$router.push({ name: "tdDoctorMeAppointmentItem" });
          }
        );
      } else {
        DoctorAppointmentItemService.addAppointmentItem(params).then(
          response => {
            if (response !== null && response.Data > 0) {
              this.toast$("保存成功");
            } else {
              this.toast$("保存失败");
            }
            this.indicator$.close();
            this.$router.push({ name: "tdDoctorMeAppointmentItem" });
          }
        );
      }
    }
  }
};
</script>

